//package com.example.demohello.service.impl;
//
//import com.example.demohello.entity.Course;
//import com.example.demohello.mapper.CourseMapper;
//import com.example.demohello.service.CourseService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.ValueOperations;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//
//@Service
//public class CourseServiceImpl_back implements CourseService {
//
//    @Autowired
//    CourseMapper courseMapper;
//
//    @Autowired
//    private RedisTemplate redisTemplate;
//
//
//   private static  final  String KEY = "all_course";
//    @Override
//    public Integer addCourse(Course course) {
//         redisTemplate.delete(KEY);
//         return courseMapper.addCourse(course);
//
//    }
//
//    @Override
//    public Integer deleteCourse(Integer courseId) {
//       Integer result =  courseMapper.deleteCourse(courseId);
//       if(result>0){
//           String key = "course_"+courseId;
//           if(redisTemplate.hasKey(key)){
//               redisTemplate.delete(key);
//               redisTemplate.delete(KEY);
//           }
//       }
//        return result;
//    }
//
//    @Override
//    public List<Course> selectCourse() {
//        ValueOperations<String,List<Course>> operations = redisTemplate.opsForValue();
//        if(redisTemplate.hasKey(KEY)){
//            List<Course> courseList  =  operations.get(KEY);
//            System.out.println("selectForCourseList 方法从redis获取的数据: "+courseList.toString());
//            return  courseList;
//        }
//        else{
//            List<Course> courseList  =  courseMapper.selectForCourseList();
//            redisTemplate.opsForValue().set(KEY,courseList);
//            System.out.println("selectForCourseList 方法从mysql获取的数据: "+courseList.toString());
//            return  courseList;
//        }
//    }
//
//    @Override
//    public Course selectOneCourse(Integer courseId) {
//        String key = "course_"+courseId;
//        //包装数据类型
//        ValueOperations<String,Course> operations = redisTemplate.opsForValue();
//        //如果KEY存在直接返回
//        if(redisTemplate.hasKey(key)){
//            Course course  =  operations.get(key);
//            System.out.println("selectOneCourse方法从redis获取的数据 : "+course.toString());
//            return  course;
//        }
//        else { //不存在就查询数据库获取数据，数据不为空就插入REDIS缓存中
//            Course course  =  courseMapper.selectOneCourse(courseId);
//            System.out.println("selectOneCourse方法从mysql获取的数据 : "+course.toString());
//            if(null!=course)
//                  redisTemplate.opsForValue().set(key,course);
//            return course;
//        }
//    }
//
//    @Override
//    public Integer updateCourse(Course course) {
//       Integer result =  courseMapper.updateCourse(course);
//       if(result>0) {
//           String key = "course_" + course.getCourseId();
//           if(redisTemplate.hasKey(key)) {
//               redisTemplate.delete(key);
//               redisTemplate.delete(KEY);
//               redisTemplate.opsForValue().set(key, course);
//           }
//       }
//        return result;
//    }
//
//    @Override
//    public List<Course> selectForCourseList() {
//
//        ValueOperations<String,List<Course>> operations = redisTemplate.opsForValue();
//        if(redisTemplate.hasKey(KEY)){
//            List<Course> courseList  =  operations.get(KEY);
//            System.out.println("selectForCourseList 方法从redis获取的数据: "+courseList.toString());
//            return  courseList;
//        }
//        else{
//            List<Course> courseList  =  courseMapper.selectForCourseList();
//            redisTemplate.opsForValue().set(KEY,courseList);
//            System.out.println("selectForCourseList 方法从mysql获取的数据: "+courseList.toString());
//            return  courseList;
//        }
//    }
//}
